﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Defines a base class to provide resources' translation.
    /// </summary>
    public abstract class LocalizationProvider
    {
        //Constructors

        /// <summary>
        /// Creates a new instance of <see cref="LocalizationProvider"/> class.
        /// </summary>
        /// <param name="token">Token to localize.</param>
        /// <param name="onLocalization">Callback to call when the <see cref="LocalizationProvider"/> has completed the localization.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown whether <paramref name="token"/> parameter is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown whether <paramref name="onLocalization"/> parameter is null.
        /// </exception>
        public LocalizationProvider(DynamicResourceToken token, LocalizationCompletionCallback onLocalization)
	    {
            if (token == null)
            {
                throw new ArgumentNullException("token parameter shoul dn't be either null.");
            }
            else if (onLocalization == null)
            {
                throw new ArgumentNullException("onLocalization parameter shoul dn't be either null.");
            }
            this.Token = token;
            this.OnLocalization = onLocalization;
	    }

        //Properties

        /// <summary>
        /// Called when the <see cref="LocalizationProvider"/> has completed the localization.
        /// </summary>
        private LocalizationCompletionCallback OnLocalization { get; set; }

        /// <summary>
        /// Gets the <see cref="DynamicResourceToken"/>.
        /// </summary>
        protected DynamicResourceToken Token { get; private set; }

        //Methods

        /// <summary>
        /// Localize the input <paramref name="dynamicResourceToken"/> for the input <paramref name="language"/>.
        /// </summary>
        public abstract void Localize();

        /// <summary>
        /// Publisehs the localization.
        /// </summary>
        protected void PublishLocalization()
        {
            this.Token.IsLocalized = true;
            this.PublishOnLocalization();
        }

        /// <summary>
        /// Publishes OnLocalization event.
        /// </summary>
        internal void PublishOnLocalization() 
        {
            LocalizationCompletionCallback handle = this.OnLocalization as LocalizationCompletionCallback;
            if (handle != null)
            {
                this.OnLocalization(this.Token);
            }
        }
    }
}